<?php

class Zy_search extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		header("Content-type:text/html;charset=utf-8");
		
		$this->load->library('pagination');
		$this->load->helper('url');
		$this->load->helper('form');
		
		$this->load->database();
	}

	function findAgeNum($str='')
	{
		$str = trim($str);
		
		if (empty($str))
		{
			return -1000;
		}
		
		$temp = array('1','2','3','4','5','6','7','8','9','0');
		
		$result = '';
		
		for ($i=0; $i < strlen($str); $i++)
		{
			if (in_array($str[$i],$temp))
			{
				$result.=$str[$i];
			}
		}

		if (empty($result))
		{
			$result = -1000;
		}

		if (strstr($str, "月"))
		{
			$result -= 1000;
			
			if ($result < -1000)
			{
				$result = -1000;
			}
		}
		
		return $result;
	}

	function seperate_key($str='', &$keyarr)
	{
		$str = trim($str);

		if(empty($str))
		{
			return -1;
		}
		
		$str .= ' ';

		$j = 0;
		$result = '';
		$quoteflag = 0;

		for($i = 0; $i < strlen($str); $i++)
		{
			if (
				($quoteflag && ($str[$i] != '"')) ||
				(!$quoteflag && $str[$i] != ' ')
				)
			{
				if ($str[$i] == '"')
				{
					$quoteflag = 1;
				}
				
				$j++;
				$result .= $str[$i];
			}
			else
			{
				if ($quoteflag)
				{
					$j++;
					$result .= $str[$i];
					$quoteflag = 0;
				}

				if (!$j)
				{
					continue;	
				}

				$strarr = explode(':', $result, 2);
				
				if (count($strarr) == 2)
				{
					foreach ($keyarr as $k => &$v)
					{
						if (!strcmp($strarr[0], $k))
						{
							$keyarr[$strarr[0]] = trim(trim($strarr[1], '"'), ' ');
						}
					}
				}

				$j = 0;
				$result = '';
			}
		}
		
		return 0;
	}
	
	function get_cond($search_method, $search_key, $obscure_way)
	{
		$condstring = "";
		
		if (!strcmp($search_method, 'date'))
		{
			$condstring = "date=str_to_date('$search_key', '%Y-%m-%d')";
		}
		else if (!strcmp($search_method, 'month'))
		{
			$condstring = "YEAR(date)=str_to_date('$search_key', '%Y-%m') and MONTH(date)=MONTH(str_to_date('$search_key', '%Y-%m'))";
		}
		else if (!strcmp($search_method, 'year'))
		{
			$condstring = "YEAR(date)=str_to_date('$search_key', '%Y')";
		}
		else if (!strcmp($search_method, 'age'))
		{
			$arr = explode('-', $search_key, 2);
			
			if (count($arr) > 1)
			{
				$fromage = $this->findAgeNum($arr[0]);
				$toage = $this->findAgeNum($arr[1]);
				
				if ($fromage > $toage)
				{
					return $querystring;
				}
				
				$condstring = "age>=$fromage and age<=$toage";
			}
			else if (count($arr) > 0)
			{
				$age = $this->findAgeNum($arr[0]);
				$condstring = "age=$age";
			}
			else
			{
				return $condstring;
			}
		}
		else if (!strcmp($search_method, 'sex'))
		{
			$search_key_str = 'M';
			
			if (strstr($search_key, "女"))
			{
				$search_key_str = 'F';
			}
			
			$condstring = "`sex`='$search_key_str'";
		}
		else
		{
			if (!strcmp($obscure_way, "on"))
			{
				$arr = array();
				$arr = explode(" ", $search_key);
				
				for ($i = 0; $i < count($arr); $i++)
				{
					$condstring .= "$search_method like '%";
					$condstring .= $this->db->escape_like_str($arr[$i]);
					$condstring .= "%'";
					
					if ($i < count($arr) - 1)
					{
						$condstring .= " and ";
					}
				}
			}
			else
			{
				$search_key_str = $this->db->escape_like_str($search_key);
				$condstring = "`$search_method` like '%$search_key_str%'";
			}
		}
		
		return $condstring;
	}
	
	function create_search_string($search_method, $search_key, $obscure_way, $dbtable)
	{
		$querystring = "";
		
		if (!strcmp($search_method, 'comb'))
		{
			$keyarr = array(
			  'name'			=> "",
			  'date'			=> "",
			  'month'			=> "",
			  'year'			=> "",
			  'diag'			=> "",
			  'prescribe'	=> "",
			  'phonenum'	=> "",
			  'addr'			=> "",
			  'age'				=> "",
			  'sex'				=> "",
				);

			$this->seperate_key($search_key, $keyarr);
			
			$firstflag = 1;
			$condstring = "";
			
			foreach ($keyarr as $k => &$v)
			{
				if (strcmp($v, ""))
				{
					$tempcond = $this->get_cond($k, $v, $obscure_way);
					
					if (strcmp($tempcond, ""))
					{
						if (!$firstflag)
						{
							$condstring .= " and ";
						}

						$condstring .= $tempcond;
						$firstflag = 0;						
					}
				}
			}

			if (strcmp($condstring, ""))
			{
				$querystring = "select * from $dbtable where $condstring order by date desc, `index` desc";
			}
		}
		else
		{
			$condstring = $this->get_cond($search_method, $search_key, $obscure_way);
			
			if (strcmp($condstring, ""))
			{
				$querystring = "select * from $dbtable where $condstring order by date desc, `index` desc";
			}
		}

		return $querystring;
	}
	
	function index()
	{
		$search_method = $this->input->post('search_method');
		$search_key = $this->input->post('search_key');
		$search_key = Myphplib::trim_space($search_key);
		$obscure_way = $this->input->post('obscure');

		$dbtable = $this->load->get_var('dbtable');

		$querystring = $this->create_search_string($search_method, $search_key, $obscure_way, $dbtable);

		if (!strcmp($querystring, ""))
		{
			Header("Location: zy/search");  
			return;
		}

		$query = $this->db->query($querystring);
		$num = $query->num_rows();

		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}

		$pagesObj = $this->getPages($num, $search_method, $search_key, $obscure_way);

		$per = $pagesObj->per_page;
		 
    $cur = $pagesObj->cur_page;
    $cur < 0 ? $cur = 0 : $cur;

    $querystring = "$querystring LIMIT $cur, $per";

		$query = $this->db->query($querystring);
		
		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}
		
		$data['queryresult'] = $query;
		$data['resultnum'] = $num;
		$data['search_method'] = $search_method;
		$data['search_key'] = $search_key;
		
		$data['table_bgcolor'] = $this->load->get_var('table_bgcolor');
		
		$this->load->view('Zy_view_searchresult', $data);
	}
	
	function strToHex($string)   
	{   
		$hex = "";
		   
		for ($i = 0; $i < strlen($string); $i++)   
			$hex .= dechex(ord($string[$i]));
			   
		$hex = strtoupper($hex);
		   
		return $hex;   
	}
	   
	function dosearch()
	{
		$search_method = $this->input->get('search_method');
		$search_key = $this->input->get('search_key');
		$search_key = Myphplib::trim_space($search_key);
		$obscure_way = $this->input->get('obscure_way');

		$cur = $this->input->get('per_page');

		if (!$cur)
		{
			$cur = 0;
		}

		$dbtable = $this->load->get_var('dbtable');

		$querystring = $this->create_search_string($search_method, $search_key, $obscure_way, $dbtable);

		if (!strcmp($querystring, ""))
		{
			Header("Location: zy/search");  
			return;
		}
		
		$query = $this->db->query($querystring);
		$num = $query->num_rows();

		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}

		$pagesObj = $this->getPages($num, $search_method, $search_key, $obscure_way);

		$per = $pagesObj->per_page;

    $cur < 0 ? $cur = 0 : $cur;

    $querystring = "$querystring LIMIT $cur, $per";

		$query = $this->db->query($querystring);
		
		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}
		
		$data['queryresult'] = $query;
		$data['resultnum'] = $num;
		$data['search_method'] = $search_method;
		$data['search_key'] = $search_key;
		
		$data['table_bgcolor'] = $this->load->get_var('table_bgcolor');

		$this->load->view('Zy_view_searchresult', $data);
	}

	function getPages($num, $search_method, $search_key, $obscure_way)
	{
		$config['base_url'] = base_url().'/index.php?c=zy_search&m=dosearch&search_method='.$search_method.'&search_key='.$search_key."&obscure_way=".$obscure_way;
		$config['total_rows'] = $num;
		$config['per_page'] = '10';
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;

		$config['first_link'] = '首页';
  	$config['last_link'] = '末页';
  	$config['next_link'] = '下一页>';
  	$config['prev_link'] = '<上一页';

		$config['page_query_string'] = TRUE;

		$this->pagination->initialize($config);
		
		return $this->pagination;
	}
}

?>